﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

public class ConvertString
{
    static char[] intArr = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' };
    static char[] strArr = { '零', '一', '二', '三', '四', '五', '六', '七', '八', '九',};
    static char[] Chinese = { '十', '百', '千', '万', '十', '百', '千', '亿' };
    public static string Convert2Chinese(int input)
    {
        string ret = null;
        int input2 = Math.Abs(input);
        string resource = "零一二三四五六七八九";
        string unit = "个十百千万亿兆京垓秭穰沟涧正载极";
        if (input > Math.Pow(10, 4 * (unit.Length - unit.IndexOf('万'))))
        {
            throw new System.Exception("the input is too big,input:" + input);
        }
        Func<int, List<List<int>>> splitNumFunc = (val) => {
            int i = 0;
            int mod;
            int val_ = val;
            List<List<int>> splits = new List<List<int>>();
            List<int> splitNums;
            do
            {
                mod = val_ % 10;
                val_ /= 10;
                if (i % 4 == 0)
                {
                    splitNums = new List<int>();
                    splitNums.Add(mod);
                    if (splits.Count == 0)
                    {
                        splits.Add(splitNums);
                    }
                    else
                    {
                        splits.Insert(0, splitNums);
                    }
                }
                else
                {
                    splitNums = splits[0];
                    splitNums.Insert(0, mod);
                }
                i++;
            } while (val_ > 0);
            return splits;
        };

        Func<List<List<int>>, string> hommizationFunc = (data) => {
            List<StringBuilder> builders = new List<StringBuilder>();
            for (int i = 0; i < data.Count; i++)
            {
                List<int> data2 = data[i];
                StringBuilder newVal = new StringBuilder();
                for (int j = 0; j < data2.Count;)
                {
                    if (data2[j] == 0)
                    {
                        int k = j + 1;
                        for (; k < data2.Count && data2[k] == 0; k++) ;
                        //个位不是0，前面补一个零
                        newVal.Append('零');
                        j = k;
                    }
                    else
                    {
                        newVal.Append(resource[data2[j]]).Append(unit[data2.Count - 1 - j]);
                        j++;
                    }
                }
                if (newVal[newVal.Length - 1] == '零' && newVal.Length > 1)
                {
                    newVal.Remove(newVal.Length - 1, 1);
                }
                else if (newVal[newVal.Length - 1] == '个')
                {
                    newVal.Remove(newVal.Length - 1, 1);
                }

                if (i == 0 && newVal.Length > 1 && newVal[0] == '一' && newVal[1] == '十')
                {//一十 --> 十
                    newVal.Remove(0, 1);
                }
                builders.Add(newVal);
            }
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < builders.Count; i++)
            {//拼接
                if (builders.Count == 1)
                {//个位数
                    sb.Append(builders[i]);
                }
                else
                {
                    if (i == builders.Count - 1)
                    {//万位以下的
                        if (builders[i][builders[i].Length - 1] != '零')
                        {//十位以上的不拼接"零"
                            sb.Append(builders[i]);
                        }
                    }
                    else
                    {//万位以上的
                        if (builders[i][0] != '零')
                        {//零万零亿之类的不拼接
                            sb.Append(builders[i]).Append(unit[unit.IndexOf('千') + builders.Count - 1 - i]);
                        }
                    }
                }
            }
            return sb.ToString();
        };
        List<List<int>> ret_split = splitNumFunc(input2);
        ret = hommizationFunc(ret_split);
        if (input < 0) ret = "-" + ret;
        return ret;
    }
    //数字转换为中文
    public static string GetCountRefundInfoInChanese(string inputNum)
    {
        //金额
        //string[] Chinese = { "元", "十", "百", "千", "万", "十", "百", "千", "亿" };
        char[] tmpArr = inputNum.ToString().ToArray();
        string tmpVal = "";
        for (int i = 0; i < tmpArr.Length; i++)
        {
            tmpVal += strArr[tmpArr[i] - 48];//ASCII编码 0为48
            int c = tmpArr.Length - i;
            if (c >= 0)
                tmpVal += Chinese[tmpArr.Length - i];//根据对应的位数插入对应的单位
        }

        return tmpVal;
    }
    /// <summary>
    /// 转换数字
    /// </summary>
    protected static long CharToNumber(char c)
    {
        for (int i = 0; i < strArr.Length; i++)
            if (c == strArr[i])
                return i;
        return -1;
        //switch (c)
        //{
        //    case '一': return 1;
        //    case '二': return 2;
        //    case '三': return 3;
        //    case '四': return 4;
        //    case '五': return 5;
        //    case '六': return 6;
        //    case '七': return 7;
        //    case '八': return 8;
        //    case '九': return 9;
        //    case '零': return 0;
        //    default: return -1;
        //}
    }

    /// <summary>
    /// 转换单位
    /// </summary>
    protected static long CharToUnit(char c)
    {
        switch (c)
        {
            case '十': return 10;
            case '百': return 100;
            case '千': return 1000;
            case '万': return 10000;
            case '亿': return 100000000;
            default: return 1;
        }
    }
    /// <summary>
    /// 将中文数字转换阿拉伯数字
    /// </summary>
    /// <param name="cnum">汉字数字</param>
    /// <returns>长整型阿拉伯数字</returns>
    public static long ParseCnToInt(string cnum)
    {
        cnum = Regex.Replace(cnum, "\\s+", "");
        long firstUnit = 1;//一级单位                
        long secondUnit = 1;//二级单位 
        long tmpUnit = 1;//临时单位变量
        long result = 0;//结果
        for (int i = cnum.Length - 1; i > -1; --i)//从低到高位依次处理
        {
            tmpUnit = CharToUnit(cnum[i]);//取出此位对应的单位
            if (tmpUnit > firstUnit)//判断此位是数字还是单位
            {
                firstUnit = tmpUnit;//是的话就赋值,以备下次循环使用
                secondUnit = 1;
                if (i == 0)//处理如果是"十","十一"这样的开头的
                {
                    result += firstUnit * secondUnit;
                }
                continue;//结束本次循环
            }
            else if (tmpUnit > secondUnit)
            {
                secondUnit = tmpUnit;
                continue;
            }
            long num = CharToNumber(cnum[i]);
            if (num < 0)
                continue;
            result += firstUnit * secondUnit * num;//如果是数字,则和单位想乘然后存到结果里
        }
        return result;
    }
}
